package com.yj.ccs.abs.web.management.mapper;

import com.yj.ccs.abs.web.management.dto.IdListDto;
import com.yj.ccs.abs.web.management.entity.Company;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Copyright 2018 (C) Yunjian-VC
 * Created on : 2018/3/15
 * Author     : Unbelievable9
 **/
@Repository
@Mapper
public interface CompanyMapper {

    @Options(useGeneratedKeys = true, keyProperty = "item.id")
    @Insert("INSERT INTO company (`name`, short_name, area_code, address, industry, nature, representative, create_time) " +
            "VALUES (#{item.name}, #{item.short_name}, #{item.area_code}, #{item.address}, #{item.industry}, #{item.nature}, " +
            "#{item.representative}, NOW())")
    void create(@Param("item") Company item);

    @Insert("<script>" +
            "INSERT INTO company (`name`, short_name, area_code, address, industry, nature, representative, create_time) VALUES " +
            "<foreach collection = 'list' item = 'item' separator = ', '>" +
            "(#{item.name}, #{item.short_name}, #{item.area_code}, #{item.address}, #{item.industry}, #{item.nature}, #{item.representative}, NOW())" +
            "</foreach>" +
            "</script>")
    void batchCreate(@Param("list") List<Company> list);

    @Update("UPDATE `company` SET `name` = #{item.name}, short_name = #{item.short_name}, area_code = #{item.area_code}, " +
            "address = #{item.address}, industry = #{item.industry}, nature = #{item.nature}, representative = #{item.representative}" +
            "WHERE id = #{item.id}")
    void update(@Param("item") Company item);

    @Delete("<script>" +
            "DELETE FROM company WHERE id IN (" +
            "<foreach collection = 'item.id_list' item = 'id' separator = ', '>" +
            "#{id}" +
            "</foreach>" +
            ")" +
            "</script>")
    void delete(@Param("item") IdListDto item);

    @Select("SELECT * FROM company WHERE id = #{id} LIMIT 1")
    Company getById(@Param("id") Long id);

    @Select("SELECT * FROM company WHERE name = #{name} LIMIT 1")
    Company getByName(@Param("name") String id);
}
